package com.accompany.fragment.my.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.accompany.R;
import com.accompany.adapter.BaseRecyclerHolder;
import com.accompany.adapter.BaseRecyclerRefreshAdapter;
import com.accompany.api.bean.BalanceInfoBean;
import com.accompany.api.bean.BalanceListBean;
import com.accompany.api.bean.OrderComment;
import com.accompany.api.category.BalanceCategory;
import com.accompany.api.presenter.BalancePresenter;
import com.accompany.base.BaseActivity;
import com.accompany.dialog.LoginOutDialog;
import com.accompany.fragment.login.UserLoginActivity;
import com.accompany.fragment.order.activity.MineCommentsActivity;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.utils.FullScreenUtils;
import com.accompany.utils.SPUtils;
import com.accompany.utils.TimeUtils;
import com.accompany.utils.ToastUtil;
import com.accompany.view.ArcView;
import com.accompany.view.RecyclerViewLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/5/11 23 ：38
 * 项目名称：yuanban
 */
public class ShouRuMingxiActivity extends BaseActivity implements BalanceCategory.View, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.base_back)
    ImageView baseBack;
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.arcview)
    ArcView arcview;
    @BindView(R.id.account)
    TextView account;
    @BindView(R.id.status)
    TextView status;
    /*@BindView(R.id.today)
    TextView today;
    @BindView(R.id.sevendays)
    TextView sevendays;
    @BindView(R.id.month)
    TextView month;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;*/
    @BindView(R.id.card_my)
    CardView cardMy;
    @BindView(R.id.base_refresh_recycler)
    RecyclerView baseRefreshRecycler;
    @BindView(R.id.icn_image)
    ImageView icnImage;
    @BindView(R.id.relat_empty)
    RelativeLayout mEmptyLayout;
    @BindView(R.id.header_refresh)
    SmartRefreshLayout headerRefresh;
    private ShouRuMingxiActivity activity;
    private BalancePresenter mPresenter;
    private String mToken;
    private long mUserId;
    private int content = -1;
    private int pageNum = 1;
    private int clickStatus = 1;
    private double mPrice = 0.0;
    private String isAuth;
    private boolean isLoading =false;
    private LoginOutDialog dialog ;
    private List<BalanceListBean.BalanceList.IncomeList> mList = new ArrayList<>();
    private BaseRecyclerRefreshAdapter<BalanceListBean.BalanceList.IncomeList> mAdapter;

    private BalanceListBean baseBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_balance_income;
    }

    @Override
    public void initLayoutView() {
        activity = ShouRuMingxiActivity.this;
        FullScreenUtils.getInstance().fullScreen(activity, true);
        YCAppBar.setStatusBarColor(activity,
                ContextCompat.getColor(activity,
                        R.color.base_color));
        mTitleMsg.setText("收入明细");
        mPrice = getIntent().getDoubleExtra( "amount",0.0);
        account.setText("账户余额: "+mPrice+"元");
        isAuth = (String) SPUtils.get(activity, "isIdentification", "0");
        if (isAuth.equals("0")) {//离线
            status.setText("账户状态: " + "未认证");
        } else if (isAuth.equals("1")) {
            status.setText("账户状态: " + "已认证");
        }
        mToken = (String) SPUtils.get(activity, ConstantStaticUrl.mToken, "");
        SharedPreferences sharedPreferences = activity.getSharedPreferences("First", MODE_PRIVATE);
        mUserId = sharedPreferences.getLong(ConstantStaticUrl.userId, 0);
        mPresenter = new BalancePresenter(activity);
        mPresenter.getBalanceList(pageNum, mToken, mUserId);
//        mPresenter.getBalanceInfo(mToken,mUserId);
//        changeTextStyles(R.id.today);
//        mPresenter.getBalanceList(pageNum, mToken, mUserId);
        headerRefresh.setPrimaryColors(0xffff512f, 0xffffffff);
        mEmptyLayout.setVisibility(View.VISIBLE);

        RecyclerViewLayoutManager.setLinearVerticalItemDecorationManager(ShouRuMingxiActivity.this, baseRefreshRecycler);
        mAdapter = new BaseRecyclerRefreshAdapter<BalanceListBean.BalanceList.IncomeList>(ShouRuMingxiActivity.this,mList,R.layout.item_balance_view) {
            @Override
            public void convert(BaseRecyclerHolder holder, BalanceListBean.BalanceList.IncomeList item, int position, boolean isScrolling) throws IOException {
                try {
                    holder.setText(R.id.date, mList.get(position).getCreateTime().split("-")[1]+
                            "."+mList.get(position).getCreateTime().split("-")[2]);
                    holder.setText(R.id.day, TimeUtils.getWeek(mList.get(position).getCreateTime()));
                    holder.setText(R.id.income, "￥"+String.valueOf(mList.get(position).getAcdFee()));
                    if (mList.get(position).getOrder() != null){
                        holder.setText(R.id.ways, "陪诊服务—"+mList.get(position).getOrder().getSnapData1().getServiceName());
                        holder.setText(R.id.note, mList.get(position).getOrder().getSnapData1().getHospitalName()+" | "+
                                mList.get(position).getOrder().getSnapData1().getOfficeName()+" | "+
                                mList.get(position).getOrder().getSnapData1().getPatientName());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        };
        mAdapter.setDatas(mList);
        baseRefreshRecycler.setAdapter(mAdapter);
    }
    @OnClick(R.id.base_back)
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.base_back:
                finishWitchAnimation();
                break;
        }
    }

//    @OnClick({R.id.base_back,R.id.today,R.id.sevendays,R.id.month,R.id.tv_all})
//    public void OnClick(View view){
//
//        switch (view.getId()){
//            case R.id.base_back:
//                finishWitchAnimation();
//                break;
//            case R.id.today://今天
//                changeTextStyles(R.id.today);
//                break;
//            case R.id.sevendays://近7天
//                changeTextStyles(R.id.sevendays);
//                break;
//            case R.id.month://本月
//                changeTextStyles(R.id.month);
//                break;
//            case R.id.tv_all://全部
//                changeTextStyles(R.id.tv_all);
//                break;
//        }
//    }

//    public void changeTextStyles(int id){
//        pageNum= 1;
//        TextPaint tptoday = today .getPaint();
//        tptoday.setFakeBoldText(false);
//        today.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//
//        TextPaint tpsevendays = sevendays .getPaint();
//        tpsevendays.setFakeBoldText(false);
//        sevendays.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//
//        TextPaint tpmonth = month .getPaint();
//        tpmonth.setFakeBoldText(false);
//        month.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//
//        TextPaint tptvAll = tvAll .getPaint();
//        tptvAll.setFakeBoldText(false);
//        tvAll.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
//        switch (id){
//            case R.id.today:
//                tptoday.setFakeBoldText(true);
//                today.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
//                mPresenter.getBalanceList(1,mToken,mUserId,clickStatus);
//                break;
//            case R.id.sevendays:
//                tpsevendays.setFakeBoldText(true);
//                sevendays.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
//                mPresenter.getBalanceList(1,mToken,mUserId,clickStatus);
//                break;
//            case R.id.month:
//                tpmonth.setFakeBoldText(true);
//                month.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
//                mPresenter.getBalanceList(1,mToken,mUserId,clickStatus);
//                break;
//            case R.id.tv_all:
//                tptvAll.setFakeBoldText(true);
//                tvAll.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
//                mPresenter.getBalanceList(1,mToken,mUserId,clickStatus);
//                break;
//        }
//    }


    @Override
    public void getBalanceList(BalanceListBean baseBean) {
        this. baseBean = baseBean;
        if (baseBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (baseBean.getData() != null && baseBean.getData().getList().size() > 0) {
                if (pageNum == 1) {
                    if (isLoading) {
                        isLoading = false;
                    } else {
                        mList.clear();
                    }
                    mList.addAll(baseBean.getData().getList());
                    mAdapter.setDatas(baseBean.getData().getList());
                } else {
                    mList.addAll(mList.size(), baseBean.getData().getList());
                    mAdapter.add(baseBean.getData().getList());
                }
                mEmptyLayout.setVisibility(View.GONE);
                baseRefreshRecycler.setVisibility(View.VISIBLE);
            } else {
                if (isLoading) {
                    isLoading = false;
                } else {
                    mEmptyLayout.setVisibility(View.VISIBLE);
                    baseRefreshRecycler.setVisibility(View.GONE);
                }
            }
        }else if (baseBean.getCode() == ConstantStaticUrl.NO_LOGIN_CODE) {
            dialog =  LoginOutDialog.getLoginOutDialog(ShouRuMingxiActivity.this);
            dialog.show();
            dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    dialog = null;
                }
            });
            dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(UserLoginActivity.class);
                    SPUtils.clear(ShouRuMingxiActivity.this);
                    finish();
                    dialog.dismiss();
                    dialog = null;
                }
            });

        } else {
            ToastUtil.showToastINFO(ShouRuMingxiActivity.this, baseBean.getMsg());
        }


    }

    @Override
    public void getBalanceInfo(BalanceInfoBean balanceInfoBean) {
        if (balanceInfoBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
//            if (balanceInfoBean.getData() != null){
//                account.setText("账户余额: "+balanceInfoBean.getData().getAccount());
//                status.setText("账户状态: "+"待审核五笔");
//            }
        } else {
//            ToastUtil.showToastINFO(activity, balanceInfoBean.getMsg());
        }
    }

    @Override
    public void SetPersenter(Object persenter) {

    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if (baseBean.getData().isHasNextPage()) {
            isLoading = true;
            pageNum++ ;
            mPresenter.getBalanceList(pageNum, mToken, mUserId);
        }else {
            isLoading = false;
        }
        headerRefresh.finishLoadMore(500);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        mPresenter.getBalanceList(pageNum, mToken, mUserId);
        headerRefresh.finishRefresh(500);
    }
}
